package fr.mywiki.business.entry;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import junit.framework.Test;
import junit.framework.TestSuite;
import servletunit.struts.CactusStrutsTestCase;
import fr.mywiki.business.entry.EntryListManager;
import fr.mywiki.model.ejbAdapter.DataController;
import fr.mywiki.model.ejbAdapter.Value;
import fr.mywiki.model.queries.DatabaseAdapter;
import fr.mywiki.model.queries.ResultList;

/**
 * Unit test class for EntryListManager.
 * 
 * @see http://localhost:8080/MyWiki/ServletTestRunner?suite=fr.mywiki.business.entry.EntryListManagerTest&xsl=cactus-report.xsl
 */
public class EntryListManagerTest extends CactusStrutsTestCase {
	public EntryListManagerTest(String a_name) {
		super(a_name);
	}

	public static Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTestSuite(EntryListManagerTest.class);
		return suite;
	}
	
	/**
	 * Tests EntryListManager. Should return a list containing all the entry ids.
	 */
	public void testGetAllEntriesIds() {
		/* Create at least one entry */
		Value val = null;
		try {
			val = DataController.createValue(EntryLite.TYPE_ENT);
		} catch (Exception e1) {
			fail("DataController.createValue(EntryLite.TYPE_ENT)");
		}
		
		ArrayList entryIds = null;
		try {
			entryIds = new ArrayList(EntryListManager.getAllEntryIds());
		} catch (SQLException e) {
			fail("EntryListManager.getAllEntryIds();");
		}
		
		assertNotNull(entryIds);
		
		String sql = "SELECT id FROM entry";
		ResultList resultList = null;
		try {
			resultList = DatabaseAdapter.executeSelectQuery(sql);
		} catch (SQLException e) {
			fail("DatabaseAdapter.executeSelectQuery(sql)");
		}
		assertEquals(resultList.getRecordsCount(),entryIds.size());
		
		Iterator it_result = resultList.iterator();
		while (it_result.hasNext()) {
			Object[] tab = (Object[]) it_result.next();
			assertTrue(entryIds.contains((Long) tab[0]));
		}
		
		try {
			val.remove();
		} catch (Exception e) {
			fail("val.remove()");
		}
	}
	
	public void testGetRandomEntryId() {
		/* Create at least one entry */
		Value val = null;
		try {
			val = DataController.createValue(EntryLite.TYPE_ENT);
		} catch (Exception e1) {
			fail("DataController.createValue(EntryLite.TYPE_ENT)");
		}
		
		Long id = null;
		try {
			id = EntryListManager.getRandomEntryId();
		} catch (SQLException e) {
			fail("EntryListManager.getRandomEntryId()");
		}
		
		String sql = "SELECT id FROM entry";
		ResultList resultList = null;
		try {
			resultList = DatabaseAdapter.executeSelectQuery(sql);
		} catch (SQLException e) {
			fail("DatabaseAdapter.executeSelectQuery(sql)");
		}
		
		ArrayList list = new ArrayList();
		
		Iterator it_result = resultList.iterator();
		while (it_result.hasNext()) {
			Object[] tab = (Object[]) it_result.next();
			list.add((Long) tab[0]);
		}
		
		assertTrue(list.contains(id));
		
		try {
			val.remove();
		} catch (Exception e) {
			fail("val.remove()");
		}
	}
}
